package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.PointAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit2.Call;

public class PointNewActivity extends BaseActivity implements PtrHandler {
    private Context context = this;
    @BindView(R.id.pointTxt)
    TextView pointTxt;
    @BindView(R.id.pfl_root)
    PtrClassicFrameLayout pfl_root;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_point_new;
    }

    @Override
    protected void initView() {
        pfl_root.setEnabledNextPtrAtOnce(true);
        pfl_root.setLastUpdateTimeRelateObject(this);
        pfl_root.setPtrHandler(this);
        pfl_root.setKeepHeaderWhenRefresh(true);
        getMbUserInfo();

        findViewById(R.id.jfscLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(PointShopActivity.class);
            }
        });
        findViewById(R.id.jfmxLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               launch(PointActivity.class);
            }
        });
        findViewById(R.id.yqhyLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(YqhyActivity.class);
            }
        });
    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if ((linkedTreeMap.get("BALANCE_POINTS")+"").equals("null")){
                        pointTxt.setText("0");
                    }else{
                        pointTxt.setText(decimalFormat.format(linkedTreeMap.get("BALANCE_POINTS")));
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }


    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        pfl_root.refreshComplete();
        getMbUserInfo();
    }
}
